const Generator = require('yeoman-generator')

module.exports = class extends Generator {
  async prompting() {
    const appname = this.appname.replace(/\s+/g, '-')
    this.answers = Object.assign({
      appname
    }, await this.prompt([
      {
        type: 'input',
        name: 'name',
        message: `question name (${this.appname})`,
        default: appname
      },
      {
        type: 'input',
        name: 'version',
        message: 'question version (1.0.0)',
        default: '1.0.0'
      },
      {
        type: 'input',
        name: 'description',
        message: 'question description',
        default: ''
      },
      {
        type: 'input',
        name: 'main',
        message: 'question entry point (index.js)',
        default: 'index.js'
      },
      {
        type: 'input',
        name: 'respository',
        message: 'question respository url',
        default: ''
      },
      {
        type: 'input',
        name: 'author',
        message: 'question author',
        default: ''
      },
      {
        type: 'confirm',
        name: 'private',
        message: 'question private'
      }
    ]))
  }
  writing() {
    const templates = [
      'bin/index.js',
      'lib/index.js',
      '.editorconfig',
      '.gitignore',
      'LICENSE',
      'README.md',
      'package.json',
    ]
    templates.forEach(tpl => {
      if (tpl === 'LICENSE' && !this.answers.license) return
      this.fs.copyTpl(
        this.templatePath(tpl),
        this.destinationPath(tpl === 'lib/index.js' && this.answers.main !== 'index.js' ? `lib/${this.answers.main}` : tpl),
        this.answers
      )
    })
    this.log('Saved package.json')
  }
}